﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CRM.Database.Model;
using DevExpress.Web.ASPxUploadControl;
using DevExpress.Web.Mvc;
using Utilites;

namespace CRM.Controllers
{
    [Localize, CheckAccess]
    public class ContractsController : Controller
    {
        //
        // GET: /Contracts/

        public ActionResult Index()
        {
            var col = new ContractsCollection();
            return View(col.ContractsList);
        }


       public ActionResult ContractsPartial()
       {
           var col = new ContractsCollection();
           return PartialView("ContractsPartial", col.ContractsList);
       }

        public ActionResult ConditionsPartial(int customerId)
        {
            var col = new CustomersCollection();
            return PartialView(col.GetItemById(customerId) as Customers);
        }

        public ActionResult ConditionsPartialEdit(int customerId)
        {
            var col = new CustomersCollection();
            return PartialView(col.GetItemById(customerId) as Customers);
        }


       public ActionResult CustomerContractsPartial(int customerId)
       {
           var col = new CustomersCollection();
           Customers cust = col.GetItemById(customerId) as Customers;
           return PartialView(cust);
       }


       public ActionResult CustomerContractsPartialEdit(int customerId)
       {


           var col = new CustomersCollection();
           Customers cust = col.GetItemById(customerId) as Customers;

         

           return PartialView(cust);
       }

        [HttpPost]
       public ActionResult CustomerContractsPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] Database.Model.Contracts newContract, int customerId)
        {
           
           
            var contrCol = new ContractsCollection();
            newContract.IdProfineEssentialElement = 1;
            if (contrCol.Add(newContract, Request.LogonUserIdentity.Name))
            {
                var cc=new ContractsCollection();
                Contracts contr = cc.GetItemById(newContract.IdContract) as Contracts;
                CreateContractDocument(contr);
            }
            var col = new CustomersCollection();
            Customers cust = col.GetItemById(customerId) as Customers;
            return PartialView("CustomerContractsPartialEdit", cust);
        }

        [HttpPost]
        public ActionResult CustomerContractsPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] Database.Model.Contracts newContract, int customerId)
        {

            DevExpress.Web.ASPxUploadControl.UploadedFile[] files = UploadControlExtension.GetUploadedFiles("scannnn");
            var contrCol = new ContractsCollection();
            newContract.IdProfineEssentialElement = 1;
        if(    contrCol.Update(newContract, Request.LogonUserIdentity.Name))
        {
            Contracts contr = contrCol.GetItemById(newContract.IdContract) as Contracts;
            CreateContractDocument(contr);
        }
            var col = new CustomersCollection();
            Customers cust = col.GetItemById(customerId) as Customers;
            return PartialView("CustomerContractsPartialEdit", cust);
        }

        [HttpPost]
        public ActionResult CustomerContractsPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] Database.Model.Contracts newContract, int customerId)
        {
            var contrCol = new ContractsCollection();
            newContract.IdProfineEssentialElement = 1;
            contrCol.Delete(newContract);
            var col = new CustomersCollection();
            Customers cust = col.GetItemById(customerId) as Customers;
            return PartialView("CustomerContractsPartialEdit", cust);
        }


        protected void CreateContractDocument(Contracts contract)
        {
            var items = new List<DocReport.DocReportParamsItem>();

            //DateTime createDate = (DateTime)row["AgreementCreationDate"];
            //string cd = createDate.ToShortDateString();

            var item = new DocReport.DocReportParamsItem(DocBookmarks.AgreementNumber,
                                                         contract.ContractNumber);
            items.Add(item);

            item = new DocReport.DocReportParamsItem(DocBookmarks.ExpirationYear,
                                                  contract.CreationDate.Year.ToString());
            items.Add(item);

            item = new DocReport.DocReportParamsItem(DocBookmarks.LegalEntityName,
                                                     contract.OwnerShipPatternName + " " +
                                                     contract.CustomerFullName);
            items.Add(item);



            item = new DocReport.DocReportParamsItem(DocBookmarks.AgreementCreationDate, contract.CreationDate.ToShortDateString());
            items.Add(item);

            item = new DocReport.DocReportParamsItem(DocBookmarks.ClientGeneralDirectorNameInRP,
                                                     contract.AgreementElements.FIOGenDirInRP);
            items.Add(item);

            item = new DocReport.DocReportParamsItem(DocBookmarks.DirectorFIOShort,
                                                   contract.AgreementElements.FIOGenDirShort);
            items.Add(item);

            StringBuilder sb = new StringBuilder();
            sb.Append(contract.OwnerShipPatternShortName);
            sb.Append(" ");
            sb.Append(contract.CustomerFullName);
            sb.AppendLine();
            sb.Append(contract.AgreementElements.PostIndex);
            sb.Append(" ");
            sb.Append(contract.AgreementElements.FullAddress);
            sb.AppendLine();
            sb.Append("ИНН ");
            sb.Append(contract.AgreementElements.INN);
            sb.AppendLine();
            sb.Append("КПП ");
            sb.Append(contract.AgreementElements.KPP);
            sb.AppendLine();
            sb.AppendLine();
            sb.Append("р/с ");
            sb.Append(contract.AgreementElements.RS);
            sb.AppendLine();
            sb.Append("К/с ");
            sb.Append(contract.AgreementElements.KS);
            sb.AppendLine();
            sb.Append("БИК ");
            sb.Append(contract.AgreementElements.BIK);
            sb.AppendLine();
            sb.Append("В ");
            sb.Append(contract.AgreementElements.Bank);

            item = new DocReport.DocReportParamsItem(DocBookmarks.LegalEntitiesElements, sb.ToString());
            items.Add(item);

            var col = new ConfigurationsCollection();
            

            DocReport.CreateDoc(col.ContractsTemplatePath,
                                col.ContractsPath+@"\contract"+contract.IdContract+".doc", items);
        }


        protected int IdContract { get; set; }

         public ActionResult CallbacksImageUpload(int idContract)
         {
             IdContract = idContract;
             UploadControlExtension.GetUploadedFiles("ucCallbacks", ValidationSettings, ucCallbacks_FileUploadComplete);
             return null;
         }


        public static readonly ValidationSettings ValidationSettings = new ValidationSettings
                                                                           {
                                                                               AllowedFileExtensions = new string[] {".pdf"}, MaxFileSize = 20971520
                                                                           };


        public  void ucCallbacks_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            if (e.UploadedFile.IsValid)
            {
                var col = new ConfigurationsCollection();
                e.UploadedFile.SaveAs(col.ContractsScanPath + @"\ContractScan" + IdContract + ".pdf", true);


            }
        }

         public ActionResult Callbacks()
         {
             return PartialView("ContractScanAttachementPartial");
         }


         public ActionResult CustomerTransportConditions(int customerId)
        {
            var col = new CustomersCollection();

            return PartialView("CustomerTransportConditionsPartial", col.GetItemById(customerId) as Customers);


        }

         public ActionResult CustomerTransportConditionsPartial(int customerId)
         {
             var col = new CustomersCollection();

             return PartialView("CustomerTransportConditionsPartial", col.GetItemById(customerId) as Customers);


         }

        public ActionResult CustomerTransportConditionsEdit(int customerId)
        {
            var col = new CustomersCollection();

            return PartialView("CustomerTransportConditionsPartialEdit", col.GetItemById(customerId) as Customers);

 
        }


        public ActionResult CustomerTransportConditionsPartialEdit(int customerId)
        {
            var col = new CustomersCollection();

            return PartialView("CustomerTransportConditionsPartialEdit", col.GetItemById(customerId) as Customers);


        }


        public ActionResult CreditTypes()
        {
            var col = new CreditTypesCollection();
            return View("CreditTypes", col.CreditTypesList);
        }

        public ActionResult CreditTypesPartial()
        {
            var col = new CreditTypesCollection();
            return PartialView("CreditTypesPartial", col.CreditTypesList);
        }


         [HttpPost, ValidateInput(false)]
        public ActionResult CreditTypesPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] CreditTypes item)
         {
             var col = new CreditTypesCollection();
             if(ModelState.IsValid)
             {
                 col.Update(item, Request.LogonUserIdentity.Name);
             }

             return PartialView("CreditTypesPartial", col.CreditTypesList);
         }


         [HttpPost, ValidateInput(false)]
         public ActionResult CreditTypesPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] CreditTypes item)
         {
             var col = new CreditTypesCollection();
             if (ModelState.IsValid)
             {
                 col.Add(item, Request.LogonUserIdentity.Name);
             }

             return PartialView("CreditTypesPartial", col.CreditTypesList);
         }


         [HttpPost, ValidateInput(false)]
         public ActionResult CreditTypesPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] CreditTypes item)
         {
             var col = new CreditTypesCollection();
             if (ModelState.IsValid)
             {
                 col.Delete(item);
             }

             return PartialView("CreditTypesPartial", col.CreditTypesList);
         }


         public ActionResult CustomerCreditConditions(int customerId)
         {
             var col = new CustomersCollection();

             return PartialView("CustomerCreditConditionsPartial", col.GetItemById(customerId) as Customers);


         }


         public ActionResult CustomerCreditConditionsPartial(int customerId)
         {
             var col = new CustomersCollection();

             return PartialView("CustomerCreditConditionsPartial", col.GetItemById(customerId) as Customers);


         }




         public ActionResult CustomerCreditConditionsEdit(int customerId)
         {
             var col = new CustomersCollection();

             return PartialView("CustomerCreditConditionsPartialEdit", col.GetItemById(customerId) as Customers);


         }


         public ActionResult CustomerCreditConditionsPartialEdit(int customerId)
         {
             var col = new CustomersCollection();

             return PartialView("CustomerCreditConditionsPartialEdit", col.GetItemById(customerId) as Customers);


         }




         public ActionResult PriceConditionsTypes()
         {
             var col = new PriceConditionTypesCollection();
             return View("PriceConditionsTypes", col.PriceConditionTypesList);
         }

         public ActionResult PriceConditionTypesPartial()
         {
             var col = new PriceConditionTypesCollection();
             return PartialView("PriceConditionTypesPartial", col.PriceConditionTypesList);
         }



         [HttpPost, ValidateInput(false)]
         public ActionResult PriceConditionTypesPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditionTypes item)
         {
             var col = new PriceConditionTypesCollection();
             if (ModelState.IsValid)
             {
                 col.Update(item, Request.LogonUserIdentity.Name);
             }

             return PartialView("PriceConditionTypesPartial", col.PriceConditionTypesList);
         }


         [HttpPost, ValidateInput(false)]
         public ActionResult PriceConditionTypesPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditionTypes item)
         {
             var col = new PriceConditionTypesCollection();
             if (ModelState.IsValid)
             {
                 col.Add(item, Request.LogonUserIdentity.Name);
             }

             return PartialView("PriceConditionTypesPartial", col.PriceConditionTypesList);
         }


         [HttpPost, ValidateInput(false)]
         public ActionResult PriceConditionTypesPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditionTypes item)
         {
             var col = new PriceConditionTypesCollection();
             if (ModelState.IsValid)
             {
                 col.Delete(item);
             }

             return PartialView("PriceConditionTypesPartial", col.PriceConditionTypesList);
         }


        public ActionResult CustomerPriceConditionsPartial(int customerId)
        {
            var col = new CustomersCollection();
            return PartialView("CustomerPriceConditionsPartial", col.GetItemById(customerId) as Customers);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerTransportConditionsPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] TransportConditions item, int customerId)
        {
            var col = new TransportConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers) custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Update(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerTransportConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerTransportConditionsPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] TransportConditions item, int customerId)
        {
            var col = new TransportConditionsCollection();
            var custCol = new CustomersCollection();
            //var unitsCol = new UnitsCollection();
            //int unitId = unitsCol.GetIdByName("паллетт");
            //item.IdUnit = unitId;
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Add(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerTransportConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerTransportConditionsPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] TransportConditions item, int customerId)
        {
            var col = new TransportConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Delete(item);
            }

            return PartialView("CustomerTransportConditionsPartialEdit", cust);
        }



        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerCreditConditionsPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] CreditConditions item, int customerId)
        {
            var col = new CreditConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Update(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerCreditConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerCreditConditionsPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] CreditConditions item, int customerId)
        {
            var col = new CreditConditionsCollection();
            var custCol = new CustomersCollection();
            //var unitsCol = new UnitsCollection();
            //int unitId = unitsCol.GetIdByName("паллетт");
            //item.IdUnit = unitId;
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Add(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerCreditConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerCreditConditionsPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] CreditConditions item, int customerId)
        {
            var col = new CreditConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Delete(item);
            }

            return PartialView("CustomerCreditConditionsPartialEdit", cust);
        }

        public ActionResult CustomerPriceConditionsPartialEdit(int customerId)
        {
            var col = new CustomersCollection();
            return PartialView("CustomerPriceConditionsPartialEdit", col.GetItemById(customerId) as Customers);
        }

        public ActionResult ProductCallback(int idProduct)
        {
            var prod = DB.Products.FirstOrDefault(c=>c.IdProduct==idProduct);
            return PartialView("ProductSelector",prod);
        }



        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerPriceConditionsPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditions item, int customerId)
        {
            var col = new PriceConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Update(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerPriceConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerPriceConditionsPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditions item, int customerId)
        {
            var col = new PriceConditionsCollection();
            var custCol = new CustomersCollection();
          
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Add(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerPriceConditionsPartialEdit", cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerPriceConditionsPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] PriceConditions item, int customerId)
        {
            var col = new PriceConditionsCollection();
            var custCol = new CustomersCollection();
            Customers cust = (Customers)custCol.GetItemById(customerId);
            if (ModelState.IsValid)
            {
                col.Delete(item);
            }

            return PartialView("CustomerPriceConditionsPartialEdit", cust);
        }



        public ActionResult BonusesStatuses()
        {
            var col = new BonusesStatusesCollection();
            return View("BonusesStatuses", col.BonusStatusesList);
        }

        public ActionResult BonusesStatusesPartial()
        {
            var col = new BonusesStatusesCollection();
            return PartialView("BonusesStatusesPartial", col.BonusStatusesList);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult BonusesStatusesPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] BonusStatuses item)
        {
            var col = new BonusesStatusesCollection();
            if (ModelState.IsValid)
            {
                col.Update(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("BonusesStatusesPartial", col.BonusStatusesList);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult BonusesStatusesPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] BonusStatuses item)
        {
            var col = new BonusesStatusesCollection();
            if (ModelState.IsValid)
            {
                col.Add(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("BonusesStatusesPartial", col.BonusStatusesList);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CreditTypesPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] BonusStatuses item)
        {
            var col = new BonusesStatusesCollection();
            if (ModelState.IsValid)
            {
                col.Delete(item);
            }

            return PartialView("BonusesStatusesPartial", col.GetList());
        }






        public ActionResult CustomerBonusPartial(int customerId)
        {
            var col = new CustomersCollection();

            return PartialView("CustomerBonusPartial", col.GetItemById(customerId) as Customers);


        }




        public ActionResult CustomerBonusPartialEdit(int customerId)
        {
            var col = new CustomersCollection();

            return PartialView("CustomerBonusPartialEdit", col.GetItemById(customerId) as Customers);


        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerBonusPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] Bonuses item, int customerId)
        {
            var custCol = new CustomersCollection();

            Customers cust = (Customers)custCol.GetItemById(customerId);
            var col = new BonusesCollection();
            if (ModelState.IsValid)
            {
                col.Update(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerBonusPartialEdit",cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerBonusPartialAdd([ModelBinder(typeof(DevExpressEditorsBinder))] Bonuses item, int customerId)
        {
            var custCol = new CustomersCollection();

            Customers cust = (Customers)custCol.GetItemById(customerId);
            var col = new BonusesCollection();
            if (ModelState.IsValid)
            {
                col.Add(item, Request.LogonUserIdentity.Name);
            }

            return PartialView("CustomerBonusPartialEdit",cust);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerBonusPartialDelete([ModelBinder(typeof(DevExpressEditorsBinder))] Bonuses item, int customerId)
        {
            var custCol = new CustomersCollection();

            Customers cust = (Customers)custCol.GetItemById(customerId);
            var col = new BonusesCollection();
            if (ModelState.IsValid)
            {
                col.Delete(item);
            }

            return PartialView("CustomerBonusPartialEdit", cust);
        }


        public ActionResult AllConditionsPartial(int customerId)
        {
            List<AllCondition> res;
            var col = new AllConditionsCollection();
            if(customerId==0)
            {
                res = col.AllConditionsList;
            }else
            {
                var custCol = new CustomersCollection();
                Customers cust = (Customers) custCol.GetItemById(customerId);
                res = col.GetAllConditionsByCustomer(cust);

            }

            return PartialView(res);
        }



        public ActionResult PriceConditions()
        {
            var col = new PriceConditionsCollection();

            return View(col.PriceConditionsList);
        }


        public ActionResult PriceConditionsPartial()
        {
            var col = new PriceConditionsCollection();

            return PartialView(col.PriceConditionsList);
        }


        public ActionResult AllConditions()
        {
            var col = new AllConditionsCollection();
            return View(col.AllConditionsList);
        }

        
        }
}
